<?php

namespace App\Http\Controllers\admin;

use App\Model\PayPicModel;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use App\Http\Controllers\Controller;

class PaytypeController extends Controller
{
	//logo首页
	public function index(){
		$paytype = PayPicModel::where(function($query){
			if(request()->keywords){
				$query->where('name',request()->keywords);
			}
		})->where('type',1)->orderBy('id', 'desc')->paginate(10);
		//分页
		$page = $paytype->links();
		return view('admin.paytype.index',compact('paytype','page'))->with('keywords',request()->keywords);
	}

	//添加页
	public function add(){
		return view('admin.paytype.add');
	}

	//添加
	public function insert(Request $request){
		DB::beginTransaction();
		try{
			//添加银行卡信息
			PayPicModel::action_insertt($request->all());
			DB::commit();
			return ['code'=>1,'msg'=>'添加成功'];
		}catch(Exceptation $e){
			DB::rollBack();
			return ['code'=>0,'msg'=>'添加失败'];
		}
	}

	//编辑(加载信息)
	public function edit(){
		$list = PayPicModel::where('id',request()->id)->first();

		return view('admin.paytype.edit',compact('list'));
	}

	//编辑
	public function update(Request $request){
		DB::beginTransaction();
		try{
			//添加银行卡信息
			PayPicModel::action_update($request->all());
			DB::commit();
			return ['code'=>1,'msg'=>'编辑成功'];
		}catch(Exceptation $e){
			DB::rollBack();
			return ['code'=>0,'msg'=>'编辑失败'];
		}
	}

	//删除
	public function del(){
		DB::beginTransaction();
		try{
			$data = PayPicModel::list(request()->id);
			@unlink(".".$data['logo']);
			@unlink(".".$data['logo_bg']);
			//添加银行卡信息
			PayPicModel::where('id',request()->id)->delete();
			DB::commit();
			return ['code'=>1,'msg'=>'删除成功'];
		}catch(Exceptation $e){
			DB::rollBack();
			return ['code'=>0,'msg'=>'删除失败'];
		}
	}

}